<?php

/*
 * This file is part of the Dektrium project.
 *
 * (c) Dektrium project <http://github.com/dektrium/>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace yiike\user\models;

use Yii;

/**
 * SettingsForm gets user's username, email and password and changes them.
 *
 * @property User $user
 *
 * @author Dmitry Erofeev <dmeroff@gmail.com>
 */
class Profile extends \dektrium\user\models\Profile {

    public $timezone;
    public $avatar_upload;

/*     public function behaviors() {
        return [
            [
                'class'            => 'trntv\filekit\behaviors\UploadBehavior',
                'multiple'         => false,
                'attribute'        => 'avatar_upload',
                'pathAttribute'    => 'avatar',
                'baseUrlAttribute' => 'base_url',
            ]
        ];
    } */

    public function rules() {
        $rules = parent::rules();
        $rules += [
            [['avatar', 'avatar_upload', 'base_url', 'sex', 'height', 'weight', 'birthyear'], 'safe'],
        ];
        return $rules;
    }

    public function attributeLabels() {
        $labels                  = parent::attributeLabels();
        $labels['avatar']        = '头像';
        $labels['base_url']      = '头像域名';
        $labels['avatar_upload'] = '头像';
        $labels['sex']           = '性别';
        $labels['height']        = '身高';
        $labels['weight']        = '体重';
        $labels['birthyear']     = '生日';
        return $labels;
    }

    function afterFind() {
        return parent::afterFind();
    }

    function beforeSave($insert) {
        $this->timezone = new \DateTimeZone('Asia/Shanghai');
        return parent::beforeSave($insert);
    }

    public function setAvatarByUrl($url) {
		$this->base_url = '';
        $this->avatar = $url;
		/* 
        $curl      = new \yiike\core\helpers\Curl;
        $imageFile = $curl->get($url);
        if ($imageFile) {
            $uid            = uniqid(time()) . $this->user_id;
            $fileName       = $uid . '.jpg';
            $this->base_url = \Yii::$app->fileStorage->baseUrl;
            $this->avatar   = 'avatar/' . $fileName;
            Yii::$app->fs->write($this->avatar, $imageFile);
        } */
    }

    public function setAvatarFile($imageFile) {
        if ($imageFile) {
            $uid      = uniqid(time()) . $this->user_id;
            $fileName = $uid . '.' . $imageFile->extension;
            $filePath = Yii::$app->fs->path . '/avatar/' . $fileName;
            if ($imageFile->saveAs($filePath)) {
                $this->base_url = \Yii::$app->fileStorage->baseUrl;
                $this->avatar   = 'avatar/' . $fileName;
            }
        }
    }

    public function getAvatarUrl($size = 200) {
        $pre = substr($this->avatar, 0, 6);
        if ($pre == 'http:/' || $pre == 'https:') {
            return $this->avatar;
        }
        else {
            return $this->avatar ? $this->base_url . $this->avatar : '';
        }
    }

    public static function getOptionsByRole($role) {
        $users   = \Yii::$app->authManager->getUserIdsByRole($role);
        $options = array();
        foreach ($users as $v) {
            $u = self::findOne($v);
            if ($u) {
                $options[$v] = $u->name;
            }
        }
        return $options;
    }

}
